const fetch = require('node-fetch')

async function testRateLimit () {
  console.log('开始测试API限流...')

  const baseUrl = 'http://localhost:3000'
  const testEndpoint = '/api/auth/me'

  try {
    // 连续发送多个请求测试限流
    for (let i = 1; i <= 5; i++) {
      console.log(`发送第 ${i} 个请求...`)

      const response = await fetch(`${baseUrl}${testEndpoint}`, {
        method: 'GET',
        headers: {
          'Content-Type': 'application/json'
        }
      })

      console.log(`请求 ${i}: 状态码 ${response.status}`)

      if (response.status === 429) {
        console.log('✅ 限流正常工作 - 收到429状态码')
        const text = await response.text()
        console.log('响应内容:', text)
        break
      } else if (response.status === 401) {
        console.log('✅ 未登录状态正常 - 收到401状态码')
      } else {
        const result = await response.json()
        console.log('响应:', result)
      }

      // 短暂延迟
      await new Promise(resolve => setTimeout(resolve, 100))
    }
  } catch (error) {
    console.error('测试失败:', error.message)
  }
}

testRateLimit()
